package com.bjpowernode.p2p.service.impl.loan;

import com.alibaba.dubbo.config.annotation.Service;
import com.bjpowernode.p2p.mapper.loan.RechargeRecordMapper;
import com.bjpowernode.p2p.mapper.user.FinanceAccountMapper;
import com.bjpowernode.p2p.model.loan.RechargeRecord;
import com.bjpowernode.p2p.service.loan.RechargeRecordService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ClassName:RechargeRecordServiceImpl
 * Package:com.bjpowernode.p2p.service.impl.loan
 * Description: 描述信息
 *
 * @date:2021/9/3 15:01
 * @author:sjf
 * @Copyright:动力节点
 */

@Component
@Service(interfaceClass = RechargeRecordService.class, version = "1.0.0", timeout = 15000)
public class RechargeRecordServiceImpl implements RechargeRecordService {

    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;

    @Autowired
    private FinanceAccountMapper financeAccountMapper;

    @Override
    public int addRecharge(RechargeRecord rechargeRecord) {
        return rechargeRecordMapper.insertSelective(rechargeRecord);
    }

    @Override
    public int doWithKQNotify(String orderId, Integer payResult) {
        if(ObjectUtils.anyNotNull(orderId, payResult)) {
            //1. 查询订单号是否商家订单
            RechargeRecord rechargeRecord = rechargeRecordMapper.selectRechargeByOrderId(orderId);
            //2. 判断订单是否处理过，没有处理过的才能处理
            if (ObjectUtils.anyNotNull(rechargeRecord)) {
                //3. 更新账户余额
                financeAccountMapper.updateFinanceAccoutByRecharge(rechargeRecord.getUid(),rechargeRecord.getRechargeMoney());
                //4. 修改充值表中的充值状态
                rechargeRecord.setRechargeStatus("1");
                rechargeRecordMapper.updateByPrimaryKeySelective(rechargeRecord);
            }

        }



        return 1;
    }
}
